var aj = {
    showTip: function(value) {
      var alertBox = document.createElement("div");
      alertBox.className = "alert";
      alertBox.textContent = value;
      

  
      document.body.appendChild(alertBox);
  
      setTimeout(function() {
        alertBox.remove();
      }, 1000);
    },
    checkDevice:function(){
        return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
    },
    isUrl:function(str){
        // var pattern = /^(https?:\/\/)?(www\.)?[a-zA-Z0-9-]+\.[a-zA-Z0-9]+(\.[a-zA-Z0-9]+)*\/?$/;
        // return pattern.test(str);
        
        var link = document.createElement("a");
        console.log("link;",link,link.href);
        link.href = str;
        
        if (link.href === str) {
            return true;
        } else {
            return false;
        }
    },
    randomColor:function(){
        var root = document.documentElement;
        var colors = [
        getComputedStyle(root).getPropertyValue('--blue'),
        getComputedStyle(root).getPropertyValue('--indigo'),
        getComputedStyle(root).getPropertyValue('--purple'),
        getComputedStyle(root).getPropertyValue('--pink'),
        getComputedStyle(root).getPropertyValue('--red'),
        getComputedStyle(root).getPropertyValue('--orange'),
        getComputedStyle(root).getPropertyValue('--yellow'),
        getComputedStyle(root).getPropertyValue('--green'),
        getComputedStyle(root).getPropertyValue('--teal'),
        getComputedStyle(root).getPropertyValue('--cyan'),
        getComputedStyle(root).getPropertyValue('--white'),
        getComputedStyle(root).getPropertyValue('--gray'),
        getComputedStyle(root).getPropertyValue('--gray-dark'),
        getComputedStyle(root).getPropertyValue('--primary'),
        getComputedStyle(root).getPropertyValue('--secondary'),
        getComputedStyle(root).getPropertyValue('--success'),
        getComputedStyle(root).getPropertyValue('--info'),
        getComputedStyle(root).getPropertyValue('--warning'),
        getComputedStyle(root).getPropertyValue('--danger'),
        getComputedStyle(root).getPropertyValue('--light'),
        getComputedStyle(root).getPropertyValue('--dark')
        ];
        return colors[Math.floor(Math.random() * colors.length)].trim();

    },
    readWord:function(word){
        console.log("read:",word);
        const pronunciationApi = 'https://dict.youdao.com/dictvoice?audio=';
        
        const newWord = word.trim();
        if (newWord !== '') {
            //const pronunciation = 'uk'; // 将此处设置为需要的发音类型
            const soundSrc = `${pronunciationApi}${newWord}&type=1`
            /*
            audioPlayer.onloadedmetadata = function() {
                audioPlayer.play();
            };
            
            audioPlayer.src = soundSrc;
            // 注意：如果音频已经在之前被加载过一次，可以使用 audioPlayer.load() 方法来重置播放器状态以确保每次都可以正常播放
            audioPlayer.load();
            */
            const audioPlayer = new Audio(soundSrc);
            audioPlayer.play();

        }
    },
    showLoading:function(){
        document.querySelector('.loading').style.display = 'flex';
    },
    hideLoading:function(){
        document.querySelector('.loading').style.display = 'none';
    },
    //删除tag关系，此处适用于删除自己添加的单词表
    deleteBook:function(bookId,typeId){
        console.log("bookId:",bookId);
        console.log("typeId:",typeId);

        this.postUrl('/QuickResponse/delete_book/',{'bookId':bookId,'typeId':typeId},function(data){
            console.log("data:",data);
            // 刷新页面
            window.location.reload();
        })
    },
    getCSRFToken:function(){
        const csrfCookieName = 'csrftoken';
        const cookieValue = document.cookie
            .split(";")
            .find(cookie => cookie.trim().startsWith(csrfCookieName + "="));
    
        if (cookieValue) {
            return cookieValue
                .split('=')[1]
                .trim();
        }
    
        return null; // 如果找不到CSRF令牌cookie则返回null
    },
    postUrl:function(url,data,callback){
        console.log("postUrl:",url,data);
        const csrftoken=this.getCSRFToken();
        console.log("csrftoken:",csrftoken);
        $.ajax({
            url:url,
            type:"POST",
            headers:{'X-CSRFToken':csrftoken},
            data:JSON.stringify(data),
            contentType:'application/json',
            success:function(response){
                console.log(response);
                aj.showTip(response.message);
                if(response.status==200){
                    callback(response.message);
                    
                }
            },
            error:function(error){
                console.log(error);
            }

        })
    }
  };